<template>
  <div id="container"></div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
import bus from "@/bus/bus.js";
window._AMapSecurityConfig = {
  securityJsCode: '9ed24b3bec987a1a78a12e54abf3a822'
}
export default {
  name: "MapContainer",
  data() {
    return {
      map: null,
      autoOptions: {
        input: ''
      },
      auto: null,
      placeSearch:null,
      searchPlaceInput: ''
    }
  },
  created() {
    bus.$on('shareUserInput', val => {
      this.autoOptions.input = val.userInput
      console.log(this.autoOptions.input)
      console.log("----------------------")
    })
  },
  methods: {
    initMap(){
      AMapLoader.load({
        key:"\te6e6375293f503cc1a8ade945bc122fc",             // 申请好的Web端开发者Key，首次调用 load 时必填
        version:"2.0",      // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins:['AMap.Scale','AMap.AutoComplete','AMap.PlaceSearch'],       // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      }).then(AMap=>{
        this.map = new AMap.Map("container",{  //设置地图容器id
          viewMode:"3D",    //是否为3D地图模式
          zoom:11,           //初始化地图级别
          center:[109.42,24.33], //初始化地图中心点位置
          resizeEnable: true,
        })
        this.map.addControl(new AMap.Scale())
        this.auto = new AMap.AutoComplete(this.autoOptions)
        this.placeSearch = new AMap.PlaceSearch({
          map: this.map
        }) //构造地点查询类
        this.auto.on('select', this.select)
      }).catch(e=>{
        console.log(e);
      })
    },
    select(e) {
      this.placeSearch.setCity(e.poi.adcode)
      this.placeSearch.search(e.poi.name) //关键字查询查询
    }
  },
  mounted() {
    // DOM初始化完成进行地图初始化
    this.initMap()
  }
}
</script>

<style scoped>
  #container {
    width: 100%;
    height: 800px;
  }
</style>